package com.mars.basic.service;


import com.ag.utils.CollectionUtils;
import com.ag.utils.JsonUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mars.basic.allmain.po.Menu;
import com.mars.basic.allmain.po.QMenu;
import com.mars.basic.allmain.vo.menu.MenuTree;
import com.mars.basic.allmain.vo.menu.MenuVo;
import com.mars.basic.allmain.vo.menu.MenuSearch;
import com.mars.basic.repo.MenuRepository;
import com.mars.common.utils.QAssert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author Marvrick
 */
@Service
public class MenuService {

    @Autowired
    private MenuRepository menuRepository;

    public MenuRepository getMenuRepository() {
        return menuRepository;
    }

    public void saveMenu(MenuVo vo) {
        Menu menu = vo.build();
        menu.setCreateTime(new Date());
        menu.setModule("basic");
        Menu m = getMenuRepository().findOneIfMutil(QMenu.menu.module.eq("basic").and(QMenu.menu.identify.eq(menu.getIdentify())));
        QAssert.isNull(m, 993, "该唯一标识已存在");
        getMenuRepository().saveAndFlush(menu);
    }

    public Page getMenuPage(String type, MenuSearch menuSearch) {
        QMenu q = QMenu.menu;
        QPageRequest of = QPageRequest.of(menuSearch.getPageNum(), menuSearch.getPageSize());
        Page<Menu> menuPage = getMenuRepository().findAll(q.type.eq(type).and(q.module.eq("basic")), of);
        return menuPage;
    }

    /**
     * 获取菜单的树形结构
     */
    public JSONArray getMenuList() {
        QMenu q = QMenu.menu;
        List<Menu> menuList = getMenuRepository().findAll(q.module.eq("basic"));
        List<MenuTree> menuTrees = new ArrayList<>(menuList.size());
        BeanUtils.copyProperties(menuList, menuTrees);


        JSONArray jsonArray = JsonUtils.listToTree(menuList, new JsonUtils.IListToTreeWrapper<Menu>() {
            @Override
            public Object getParentValue(Menu menu) {
                return menu.getPId();
            }

            @Override
            public Object getPkValue(Menu menu) {
                return menu.getId();
            }

            @Override
            public JSONObject doConvertJson(Menu menu) {
                JSONObject jsonObject = JsonUtils.formObject(menu);
                return jsonObject;
            }
        });
        return jsonArray;
    }

    public List<MenuTree> getMenuTreeList(List<MenuTree> menuTrees, List<MenuTree> all) {
        List<MenuTree> treeList = menuTrees.stream().map(o -> {
            List<MenuTree> child = all.stream().filter(q -> q.getPId().equals(o.getId())).collect(Collectors.toList());

            if (CollectionUtils.isNotEmpty(child) && child.size() > 0) {
                child = getMenuTreeList(child, all);
            }
            o.setChildren(child);
            return o;
        }).collect(Collectors.toList());
        return treeList;
    }
}
